import 'package:flutter/material.dart';
import 'package:flutter_ahj_app/base/cunchu/UserInfoUtils.dart';
import 'package:flutter_ahj_app/base/utils/JUIKit/JText.dart';
import 'package:flutter_ahj_app/base/utils/ShowFiles/GetImageWidget.dart';
import 'package:flutter_ahj_app/base/values/SQColor.dart';
import 'package:flutter_ahj_app/base/values/dimens.dart';

typedef callBack = Function();

typedef loginCallBack = Function();

///登录

class UserInfo extends StatefulWidget {
  var name; //姓名
  var introduce; //介绍
  var avatarPath; //头像地址
  var isCert; //是否认证
  callBack onTap;
  loginCallBack loginTap; //点击登录的回调
  loginCallBack payTap; //点击开通、续费的回调
  var isVip;

  UserInfo(
      {Key key,
      this.name,
      this.introduce,
      this.avatarPath,
      this.isCert,
      this.onTap,
      this.loginTap,
      this.payTap,
      this.isVip})
      : super(key: key);

  @override
  State<StatefulWidget> createState() => StateUserInfo();
}

class StateUserInfo extends State<UserInfo> {
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => widget.onTap,
      child: Container(
        padding: EdgeInsets.only(top: Dimens.dimens16, bottom: Dimens.dimens16),
        child: Container(
          width: double.infinity,
          child: Row(
            children: <Widget>[
              Container(
                height: 60,
                width: 60,
                child: ClipOval(
                  child: widget.avatarPath == null
                      ? Image.asset(
                          'img/iv_head_none.png',
                          height: Dimens.dimens60,
                          width: Dimens.dimens60,
                          fit: BoxFit.fill,
                        )
                      : ImageWidget(
                          fileName: widget.avatarPath,
                          width: Dimens.dimens60,
                          height: Dimens.dimens60,
                          boxFit: BoxFit.fill),
                ),
              ),
              Expanded(
                  child: Container(
                margin: EdgeInsets.only(left: Dimens.dimens16),
                height: 80,
                alignment: Alignment.centerLeft,
                child: UserInfoUtils.getUserToken().length > 0
                    ? _content()
                    : FlatButton(
                        onPressed: () => widget.loginTap(),
                        child: new Container(
                          alignment: Alignment.center,
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: <Widget>[
                              JText(
                                "登录/注册",
                                color: SQColor.black_333333,
                              ),
                              JText(
                                "登录可以使用更多功能",
                                color: SQColor.black_333333,
                              )
                            ],
                          ),
                        ),
                      ),
              ))

              ///姓名
              ,
            ],
          ),
        ),
      ),
    );
  }

  _content() {
    Widget w = JText("" + widget.name,
        fontSize: Dimens.textSize32,
        fontWeight: FontWeight.bold,
        textAlign: TextAlign.center,
        color: SQColor.black_333333);
    if (widget.isVip != null &&
        widget.isVip['globalStatus'] != null &&
        widget.isVip['globalStatus'] == 1) {
      bool isOpen =
          widget.isVip['userStatus'] != null && widget.isVip['userStatus'] == 1;
      w = Container(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                isOpen
                    ? RichText(
                        text: TextSpan(children: [
                          TextSpan(
                              text: widget.name,
                              style: TextStyle(
                                  fontSize: Dimens.textSize32,
                                  color: SQColor.black_333333)),
                          TextSpan(
                              text: "  ",
                              style: TextStyle(
                                  fontSize: Dimens.textSize32,
                                  color: SQColor.black_333333)),
                          WidgetSpan(
                              child: Image.asset(
                            "img/img_mine_vip.png",
                            width: Dimens.dimens22,
                            height: Dimens.dimens14,
                          )),
                        ]),
                      )
                    : JText("" + widget.name,
                        fontSize: Dimens.textSize32,
                        textAlign: TextAlign.center,
                        color: SQColor.black_333333),
                Container(
                  margin: EdgeInsets.only(top: Dimens.dimens8),
                  padding: EdgeInsets.fromLTRB(Dimens.dimens8, Dimens.dimens2,
                      Dimens.dimens8, Dimens.dimens2),
                  decoration: BoxDecoration(
                    color: SQColor.getColor("#2E7ED7"),
                    borderRadius: new BorderRadius.all(
                        new Radius.circular(Dimens.dimens16)),
                  ),
                  child: JText(
                      isOpen
                          ? "剩余天数：${widget.isVip['surplusDay'] == null ? 0 : widget.isVip['surplusDay']}天"
                          : "未开通会员",
                      color: SQColor.black_333333,
                      fontSize: Dimens.textSize20),
                ),
              ],
            ),
            GestureDetector(
              child: isOpen
                  ? Image.asset("img/img_mine_vip_pay.png",
                      width: Dimens.dimens72, height: Dimens.dimens30)
                  : Container(
                      width: Dimens.dimens72,
                      height: Dimens.dimens30,
                      child: Stack(
                        children: [
                          Image.asset("img/bac_mine_vip_open.png",
                              width: Dimens.dimens72, height: Dimens.dimens30),
                          Center(
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Image.asset("img/img_mine_vip_open.png",
                                    width: Dimens.dimens12,
                                    height: Dimens.dimens12),
                                Container(width: Dimens.dimens4),
                                JText("开通",
                                    fontSize: Dimens.textSize28,
                                    color: SQColor.black_333333)
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
              onTap: () {
                Navigator.pushNamed(context, "/VipPayView").then((res) {
                  widget.payTap();
                });
              },
            ),
          ],
        ),
      );
    }
    return w;
  }
}
